package com.lovi.user.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lovi.pojo.Address;
import com.lovi.user.mapper.AddressMapper;
import com.lovi.user.service.AddressService;
import com.lovi.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 查询地址列表
     * @param userid
     * @return
     */
    @Override
    public R list(Integer userid) {
        QueryWrapper wrapper = new QueryWrapper<Address>();
        wrapper.eq("user_id",userid);
        List<Address> list = addressMapper.selectList(wrapper);
        //这里注意，集合不是判断==null，因为空的集合对象依然不是null
        if(list.isEmpty()){
            log.info("AddressServiceImpl.list 结果为->{}", "地址查询为null");
            return R.fail(list);
        }
        log.info("AddressServiceImpl.list 结果为->{}", "地址查询ok");
        return R.ok(list);
    }

    /**
     * 增加地址
     * @param address
     * @return
     */
    @Override
    public R save(Address address) {

        int row = addressMapper.insert(address);
        if(row>0){
            return R.ok(address);
        }
        return R.fail(address);
    }

    /**
     * 删除方法
     * @param id
     * @return
     */
    @Override
    public R remove(Integer id) {
        int i = addressMapper.deleteById(id);
        if(i>0){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }
}
